package com.crealth.model;

import java.io.Serializable;

import com.alibaba.fastjson.JSONObject;
import com.crealth.memcached.CompanyCached;
import com.crealth.utils.Utils;

public class CompanyModel implements Serializable {
	private static final long serialVersionUID = 847574L;
	
	private int id;
	private String companyName;
	private int superCompanyID;
	private String companyAddr;
	private String companyPhone;
	private String shortName;
	private String cityName;
	
	public CompanyModel() {
		id = 0;
		companyName = "";
		superCompanyID = -1;
		companyAddr = "";
		companyPhone = "";
		shortName = "";
		cityName = "";
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getCompanyName() {
		return companyName;
	}
	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}
	public int getSuperCompanyID() {
		return superCompanyID;
	}
	public void setSuperCompanyID(int superCompanyID) {
		this.superCompanyID = superCompanyID;
	}
	public String getCompanyAddr() {
		return companyAddr;
	}
	public void setCompanyAddr(String companyAddr) {
		this.companyAddr = companyAddr;
	}
	public String getCompanyPhone() {
		return companyPhone;
	}
	public void setCompanyPhone(String companyPhone) {
		this.companyPhone = companyPhone;
	}	
	public String getShortName() {
		return shortName;
	}
	public void setShortName(String shortName) {
		this.shortName = shortName;
	}	
	public String getCityName() {
		return cityName;
	}
	public void setCityName(String cityName) {
		this.cityName = cityName;
	}

	public CompanyModel getTopCompany() {
		int superID = this.superCompanyID;
		while (superID > 0) {
			CompanyModel superCompany = CompanyCached.selectCompanyByID(superID);
			if (superCompany != null) {
				superID = superCompany.getSuperCompanyID();
				if (superID == 0) {
					return superCompany;
				}
			}
		}
		
		return this;
	}
	
	public CompanyModel getBelongCompany() {
		if (this.companyAddr != null && this.companyAddr.length() > 0) {
			return this;
		}
		CompanyModel superComapny = CompanyCached.selectCompanyByID(superCompanyID);
		if (superComapny != null && superComapny.getCompanyAddr().length() > 0) {
			return superComapny;
		}
		
		return this;
	}
	
	public Integer getBelongCompanyID() {
		CompanyModel belongCom = getBelongCompany();
		if (belongCom != null) {
			return belongCom.getId();
		}
		return -1;
	}
	
	public void parseFromJson(JSONObject json) {
		id = Utils.getJsonIntValue(json, "id", 0);
		superCompanyID = Utils.getJsonIntValue(json, "superCompanyID", -1);
		companyName = Utils.getJsonString(json, "companyName");
		companyAddr = Utils.getJsonString(json, "companyAddr");
		companyPhone = Utils.getJsonString(json, "companyPhone");
		shortName = Utils.getJsonString(json, "shortName");
		cityName = Utils.getJsonString(json, "cityName");
	}
	
	public JSONObject toJSONObject() {
		JSONObject jsonObj = new JSONObject();
		jsonObj.put("id", id);		
		jsonObj.put("companyName", companyName);
		jsonObj.put("superCompanyID", superCompanyID);
		jsonObj.put("companyAddr", companyAddr);
		jsonObj.put("companyPhone", companyPhone);
		jsonObj.put("shortName", shortName);
		jsonObj.put("cityName", cityName);
		
		return jsonObj;
	}
	
	public boolean topCompany() {
		if (this.superCompanyID == 0) {
			return true;
		}
		
		return false;
	}
	
	
}
